package com.springboot.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.commonutils.R;
import com.springboot.eduservice.entity.EduTeacher;
import com.springboot.eduservice.entity.vo.TeacherQuery;
import com.springboot.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-05
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin    //解决跨域问题
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("find")
    public R findteacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)@PathVariable String id){
        boolean byId = eduTeacherService.removeById(id);
        if (byId){
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation(value = "讲师数据分页显示")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pagelistTeacher(@PathVariable Long current,@PathVariable Long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();        //总记录数
        List<EduTeacher> records = pageTeacher.getRecords();        //数据集合
        //        Map map = new HashMap();
        //        map.put("total",total);
        //        map.put("rows",records);
        //        return R.ok().data(map);
        return R.ok().data("total",total).data("records",records);
    }

    @ApiOperation(value = "带查询的分页显示")
    @PostMapping("pageIfTeacher/{current}/{limit}")         //@RequestBody(required = false)注解作用 teacherQuery参数可以为null
    public R pageIfTeacher(@PathVariable Long current, @PathVariable Long limit,
                           @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);        //gmt_create字段要和数据库字段一样
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified",end);
        }
        //根据时间排序
        queryWrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher, queryWrapper);
        long total = pageTeacher.getTotal();    //总记录数
        List<EduTeacher> records = pageTeacher.getRecords();    //数据集合
        return R.ok().data("total",total).data("records",records);
    }

    @ApiOperation(value = "讲师添加")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("byId",byId);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean batchById = eduTeacherService.updateById(eduTeacher);
        if (batchById){
            return R.ok();
        }else {
            return R.error();
        }



    }


}

